<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Usuario
 *
 * @author gilmario
 */

namespace App\Models;

use SON\DB\Table;

class Usuario extends Table {

    protected $table = 'usuario';

    public function logar($login, $senha) {
        $query = "SELECT * FROM {$this->table} WHERE login=:login AND senha=:senha ";
        $p_query = $this->db->prepare($query);
        $p_query->execute(array(":login" => $login, ":senha" => $senha));
        $rs = $p_query->fetch();
        return $rs['login'] != "";
    }

    public function adiciona($login, $senha) {
        try {
            $this->db->beginTransaction();
            $query = "INSERT INTO {$this->table}(login,senha) values ('{$login}','{$senha}') ";
            $this->db->query($query);
            $this->db->commit();
        } catch (Exception $ex) {
            echo $ex;
            $this->db->roolBack();
        }
    }

    protected function create() {
        try {
            $this->db->beginTransaction();
            $query = "CREATE TABLE IF NOT EXISTS {$this->table}(id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, login VARCHAR(40) not null unique, senha varchar(40)) ENGINE MYISAM";
            $this->db->query($query);
            $this->db->commit();
        } catch (Exception $ex) {
            echo $ex;
            $this->db->roolBack();
        }
    }

}
